/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import models.Student;
import org.xml.sax.SAXException;

/**
 *
 * @author plato
 */
public class LoadXml {

    Document doc;
    NodeList nList;
    List<Student> listStudent;

    public Document getDoc() {
        return doc;
    }

    public void setDoc(Document doc) {
        this.doc = doc;
    }

    public NodeList getnList() {
        return nList;
    }

    public void setnList(NodeList nList) {
        this.nList = nList;
    }

    public List<Student> getListStudent() {
        return listStudent;
    }

    public void setListStudent(List<Student> listStudent) {
        this.listStudent = listStudent;
    }

    public void loadFileXml(String path) {
    }

    

    public LoadXml(String path) throws IOException {
        try {
            File fXmlFile = new File(path);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

            doc = (Document) dBuilder.parse(fXmlFile);

            NodeList nList = doc.getElementsByTagName("student");
            listStudent = new ArrayList<>();

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;

                    String firstName = eElement.getElementsByTagName("firstname").item(0).getTextContent();
                    String lastName = eElement.getElementsByTagName("lastname").item(0).getTextContent();
                    String address = eElement.getElementsByTagName("address").item(0).getTextContent();
                    String age = eElement.getElementsByTagName("age").item(0).getTextContent();
                    String dob = eElement.getElementsByTagName("dob").item(0).getTextContent();
                    String note = eElement.getElementsByTagName("note").item(0).getTextContent();
                    listStudent.add(new Student(firstName, lastName, address, Integer.parseInt(age), dob, note, temp+1));

                }
            }


        } catch (ParserConfigurationException ex) {
            Logger.getLogger(LoadXml.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(LoadXml.class.getName()).log(Level.SEVERE, null, ex);

        }

    }
}
